package GUI;

import java.awt.EventQueue;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.Panel;
import java.awt.ScrollPane;
import java.awt.List;
import java.awt.Scrollbar;

import javax.swing.*;
import java.io.*;
import javax.swing.JTextArea;
import javax.swing.text.JTextComponent;





public class UI {

	private JFrame frame;
	private JTextField textField;
	private JTextField textField_1;
	 private JFileChooser fileChooser;
	 private JTextField textField_2;
	 private JTextField textField_3;
	 private JTextArea textArea;
	 private JTable table;
	/**
	 * Launch the application.
	 */
	public static void main(String[] args) {
		EventQueue.invokeLater(new Runnable() {
			public void run() {
				try {
					UI window = new UI();
					window.frame.setVisible(true);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
	}

	/**
	 * Create the application.
	 */
	public UI() {
		initialize();
	}

	/**
	 * Initialize the contents of the frame.
	 */
	private void initialize() {
		frame = new JFrame();
		frame.setBounds(100, 100, 737, 534);
		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		frame.getContentPane().setLayout(null);
		
		JLabel lblAlfabetoDaEr = new JLabel("Alfabeto da ER");
		lblAlfabetoDaEr.setBounds(22, 15, 126, 15);
		frame.getContentPane().add(lblAlfabetoDaEr);
		
		textField = new JTextField();
		textField.setBounds(148, 13, 159, 19);
		frame.getContentPane().add(textField);
		textField.setColumns(10);
		
		JLabel lblEr = new JLabel("ER:");
		lblEr.setBounds(22, 46, 70, 15);
		frame.getContentPane().add(lblEr);
		
		textField_1 = new JTextField();
		textField_1.setBounds(148, 44, 159, 19);
		frame.getContentPane().add(textField_1);
		textField_1.setColumns(10);
		
		JButton btnSalvarEr = new JButton("Salvar ER");
		btnSalvarEr.addActionListener(new ActionListener() 
		{	public void actionPerformed(ActionEvent arg0) 
			{	saveActionPerformed(arg0);
			}

			private void saveActionPerformed(ActionEvent arg0) 
			{	fileChooser = new JFileChooser();
		        fileChooser.setSize(300, 320);
		        fileChooser.setCurrentDirectory(null);
		        fileChooser.setVisible(true);
		        String directory = "";
		        
		        if (fileChooser.showSaveDialog(frame) == JFileChooser.APPROVE_OPTION)
		        {      File file = fileChooser.getSelectedFile();
		               directory = file.getAbsolutePath();
		        }

		       try
		       {  PrintWriter outArq = new PrintWriter(
		                          new BufferedWriter(
		                                       new FileWriter(directory)));

		          if(textField_1.getText().isEmpty() == false)
		          {   //String t =  textField.getText() +  + textField_1.getText(); 
		        	    outArq.print(textField.getText() + "\r\n");
		        	    outArq.print(textField_1.getText());
		               outArq.close();
		          }
		       }
		       catch (Exception e)
		       {
		       }
				
			}
		});   //end btnSalvarER action
		
		
		btnSalvarEr.setBounds(22, 81, 117, 25);
		frame.getContentPane().add(btnSalvarEr);
		
		JLabel label = new JLabel("");
		label.setBounds(22, 153, 70, 15);
		frame.getContentPane().add(label);
		
		JButton btnCarregarEr = new JButton("Carregar ER");
		btnCarregarEr.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				loadActionPerformed(e);
				
			}

			private void loadActionPerformed(ActionEvent e) 
			{	fileChooser = new JFileChooser();
	        	fileChooser.setSize(300, 320);
	        	fileChooser.setCurrentDirectory(null);
	        	fileChooser.setVisible(true);
	        	String directory = "";
	        
	        	if (fileChooser.showOpenDialog(frame) == JFileChooser.APPROVE_OPTION)
	        	{      File file = fileChooser.getSelectedFile();
	               directory = file.getAbsolutePath();
	        	}

	        	try
	        	{ 	FileReader arq = new FileReader(directory);
				BufferedReader readArq = new BufferedReader(arq);
				textField.setText(readArq.readLine());
				textField_1.setText(readArq.readLine());
				arq.close();
	        	}
	        	catch (Exception e1)
	        	{
	        	}	
				
			}
		});
		btnCarregarEr.setBounds(171, 81, 136, 25);
		frame.getContentPane().add(btnCarregarEr);
		
		JLabel lblGramaticaRegular = new JLabel("Gramatica Regular");
		lblGramaticaRegular.setBounds(351, 15, 131, 15);
		frame.getContentPane().add(lblGramaticaRegular);
		
		textArea = new JTextArea();
		textArea.setBounds(361, 153, 346, 263);
		//JScrollPane scrollPane = new JScrollPane(textArea); 
		frame.getContentPane().add(textArea);
		
		JLabel lblNoterminais = new JLabel("Não-terminais");
		lblNoterminais.setBounds(361, 46, 109, 15);
		frame.getContentPane().add(lblNoterminais);
		
		JLabel lblTerminais = new JLabel("Terminais");
		lblTerminais.setBounds(361, 86, 70, 15);
		frame.getContentPane().add(lblTerminais);
		
		textField_2 = new JTextField();
		textField_2.setBounds(499, 44, 114, 19);
		frame.getContentPane().add(textField_2);
		textField_2.setColumns(10);
		
		textField_3 = new JTextField();
		textField_3.setBounds(499, 86, 114, 19);
		frame.getContentPane().add(textField_3);
		textField_3.setColumns(10);
		
		JLabel lblProdues = new JLabel("Produções");
		lblProdues.setBounds(361, 126, 110, 15);
		frame.getContentPane().add(lblProdues);
		
		JButton btnSalvarGr = new JButton("Salvar GR");
		btnSalvarGr.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) 
			{  saveGRAction(e);
			}
	
		});
		btnSalvarGr.setBounds(365, 437, 117, 25);
		frame.getContentPane().add(btnSalvarGr);
		
		JButton btnCarregarGr = new JButton("Carregar GR");
		btnCarregarGr.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) 
			{  loadGRAction(arg0);
			}
		});
		btnCarregarGr.setBounds(523, 437, 145, 25);
		frame.getContentPane().add(btnCarregarGr);
		
		JLabel lblAutomatoFinito = new JLabel("Automato Finito");
		lblAutomatoFinito.setBounds(31, 126, 117, 15);
		frame.getContentPane().add(lblAutomatoFinito);
		
		table = new JTable();
		table.setBounds(22, 153, 285, 263);
		frame.getContentPane().add(table);
		
		JMenuBar menuBar = new JMenuBar();
		frame.setJMenuBar(menuBar);
		
		JMenu mnFile = new JMenu("File");
		menuBar.add(mnFile);
		
		JMenuItem mntmLoad = new JMenuItem("load");
		mnFile.add(mntmLoad);
		
		
		
	}

	protected void saveGRAction(ActionEvent e) 
	{	fileChooser = new JFileChooser();
		fileChooser.setSize(300, 320);
		fileChooser.setCurrentDirectory(null);
		fileChooser.setVisible(true);
		String directory = "";

		if (fileChooser.showSaveDialog(frame) == JFileChooser.APPROVE_OPTION)
		{    File file = fileChooser.getSelectedFile();
			directory = file.getAbsolutePath();
		}

		try
		{  PrintWriter outArq = new PrintWriter(
                  new BufferedWriter(
                               new FileWriter(directory)));

		outArq.print(textField_2.getText() + "\r\n");
	    outArq.print(textField_3.getText() + "\r\n");
	   	outArq.print(textArea.getText());
	    outArq.close();
		
		}
		catch (Exception e2)
		{
		}
}	
	

	protected void loadGRAction(ActionEvent arg0) 
	{	fileChooser = new JFileChooser();
		fileChooser.setSize(300, 320);
		fileChooser.setCurrentDirectory(null);
		fileChooser.setVisible(true);
		String directory = "";

		if (fileChooser.showOpenDialog(frame) == JFileChooser.APPROVE_OPTION)
		{      File file = fileChooser.getSelectedFile();
			directory = file.getAbsolutePath();
		}

		try
		{ 	FileReader arq = new FileReader(directory);
			BufferedReader readArq = new BufferedReader(arq);
			textField_2.setText(readArq.readLine());
			textField_3.setText(readArq.readLine());
			
			String aux = "";
			while((aux = readArq.readLine()) != null)
			{	textArea.setText(aux);
			
			}
			
			arq.close();
		}
		catch (Exception e1)
		{
		}			
	}
}
